package d20chat;

import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.*;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.net.Socket;     // Used for sockets
import javax.swing.*;       // Used for ActionEvents
import java.awt.event.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.Caret;
import javax.swing.text.Document;

//import org.w3c.dom.Document;
//import org.w3c.dom.html.HTMLDocument;


/**
 * Contains all the user interface components of the main screen.
 * Consider changing to D20ChatUI for consistency
 */
public class d20chatui extends javax.swing.JFrame{
    
    /**
     * Current instance of Client.
     */
    public Client tempclient;
    private D20Chat tempMain;
    /**
     * Current instace of OutGoingMessageParser
     */
    public OutgoingMessageParser OutParse;
    //public Server tempserver;
    private User thisUser;
    /**
     * Listing of names of users (displayed in the playerlisting)
     */
    public String playernames[];    
    /**
     * Buffer of chat lines.
     */
    public ChatBuffer buffer;
    private String content;
    private JFileChooser fc = new JFileChooser(); 
    private JFileChooser notesfc = new JFileChooser(); 
    //public String htmlheader;
    
    private Component selectedComponent;    
    //player listing pop up menu
    private final JPopupMenu pbMenu = new JPopupMenu("pbutton");
    //character sheet pop up menu
    private final JPopupMenu csMenu = new JPopupMenu("csbutton");
    //notepad pop up menu
    private final JPopupMenu npMenu = new JPopupMenu("npbutton");
    
    
    /**
     * Creates new form d20chatui.
     * Handles the main GUI of the program.
     * @param tMain current instance of D20Chat
     */
    public d20chatui(D20Chat tMain) 
    {
        tempMain = tMain;
        initComponents();
        playernames = new String[0];
        buffer = new ChatBuffer(30);
        // Center the jFrame
        Dimension scrn = getToolkit().getScreenSize();
        this.setBounds( (scrn.width-getWidth())/2, (scrn.height-getHeight())/2, getWidth(), getHeight() );
        
        //content = "";
        //content = "";

        charactersheetbuttonpopup();
        createnotepadbuttonpopup();
        createplayerbuttonpopup();
        createchatinputpopup();
        createchatoutputpopup();

            FileFilter filter = new FileNameExtensionFilter("HTML File", "html", "htm");
        fc.addChoosableFileFilter(filter);
            filter = new FileNameExtensionFilter("text file", "txt");
        notesfc.addChoosableFileFilter(filter);
    }

    
///Begin Player Listing Code///
    /**
     * Creates a popup menu to go with the playerlisting
     *      Used by: d20chatui.d20chatui()
     *      Users: d20chatui.makeMenuItemPlayer(String label)
     */
    private void createplayerbuttonpopup()
    {
        pbMenu.add(makeMenuItemPlayer("Tell"));
        pbMenu.add(makeMenuItemPlayer("View Character Sheet"));
        pbMenu.add(makeMenuItemPlayer("View Profile"));
        pbMenu.add(makeMenuItemPlayer("Kick"));
        pbMenu.add(makeMenuItemPlayer("Silence"));
        pbMenu.add(makeMenuItemPlayer("Unsilence"));
        pbMenu.add(makeMenuItemPlayer("Silence All"));
        pbMenu.add(makeMenuItemPlayer("Unsilence All"));
        pbMenu.add(makeMenuItemPlayer("Transfer GM"));
        
        playerListingjList.setComponentPopupMenu(pbMenu);
    }
    
    
    /********************************************************************************
      Class: makeMenuItemPlayer()
      Description:
      Paramenters: String label
      Used by: d20chatui.createplayerbuttonpopup()
      Uses: d20chatui.playerlistingAH(java.awt.ActionEvent)
    ********************************************************************************/
    private JMenuItem makeMenuItemPlayer(String label)
    {
        //creates a menu item displayed as label
        JMenuItem item = new JMenuItem(label);
        
        //adds and actionlistener for when the menu item is used
        item.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {                
                playerlistingAH(evt);
            }
        });      
        
        //returns the menu item so it can be added to the menu
        return item;
    }
    
    
/********************************************************************************
  Class: playerlistingAH()
  Description: The action handler for the player list
  Parameters: ActionEvent evt
  Used by:
  Uses:
********************************************************************************/
    private void playerlistingAH(ActionEvent evt) 
    {        
        
        
        //Point temppoint = jList1.getMousePosition();
        //Object temp = jList1.getComponentAt(temppoint);
        Object temp = playerListingjList.getSelectedValue();
        String temps = null;
        
        //if there is a slected object
        if(temp != null)
        {
            //attach a scanner object to the selected string
            Scanner pieceScanner = new Scanner( temp.toString() );

            //find the first word and store it in temps
            if( pieceScanner.hasNext() )
            {
               temps = pieceScanner.next();
            }
        }
        
        //get the action that was selected in the player listing popup
        String action = evt.getActionCommand(); 
        
        if(action.equals("Kick"))
        {
            
            if(temps != null)
            {

                OutParse.sendMessage("/kick " + temps);
            }
        }
        else if (action.equals("Silence"))
        {
            if(temps != null)
            {

                OutParse.sendMessage("/silence " + temps + " on");
            }            
        }
        else if (action.equals("Unsilence"))
        {
            if(temps != null)
            {

                OutParse.sendMessage("/silence " + temps + " off");
            }
        }            
        else if (action.equals("Silence All"))
        {
            OutParse.sendMessage("/silence all on");
        }
        else if (action.equals("Unsilence All"))
        {
            OutParse.sendMessage("/silence all off");
        }
        else if (action.equals("Tell"))
        {
            if(temps != null)
            {
                chatEntryTextArea.setText("/tell " + temps + " " + chatEntryTextArea.getText());
                chatEntryTextArea.requestFocus();
            }
        }
        else if (action.equals("View Character Sheet"))
        {
            if(temps != null)
            {
                OutParse.sendMessage("/charsheet " + temps);
            }
        }
        else if( action.equals("View Profile"))
        {
            if(temps != null)
            {
                OutParse.sendMessage("/profile " + temps);
            }
        }
        else if ( action.equals("Transfer GM"))
        {            
            if(temps != null)
            {
                OutParse.sendMessage("/makeGM " + temps);
            }
        }
    }
    
    
/**
     * Adds a player to the playerlisting.
     * @param splayer Username of the player to be added
     * @param pcharacter Character name of the player to be added.
     */
    public void addplayer(String splayer, String pcharacter )
    {
        
        String tempplayernames[] = new String[playernames.length+1];
        String newPlayersNames = splayer + " ( " + pcharacter + " ) ";
        for(int i = 0; i < playernames.length; i++)
        {
            tempplayernames[i] = playernames[i];
        }
        tempplayernames[tempplayernames.length - 1] = newPlayersNames;
        playernames = tempplayernames;
        playerListingjList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = playernames;
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        playerListing.setViewportView(playerListingjList);
       // list1.
        //jList1.i
        //playerListing.add(splayer);
    }
    
    
/**
     * Refreshes the player listing.
     * Usually called by IncomingMessageParser on requestUserInfo.
     */
    public void refreshplayer()
    {
                playerListingjList.setModel(new javax.swing.AbstractListModel() {
                String[] strings = playernames;
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
            playerListing.setViewportView(playerListingjList);
    }
    

    /**
     * Updates the playerlisting when a player changes their name or charactername.
     *      Used by: IncomingMessageParser.request()
     * @param fromname Original name to be changed
     * @param toname new name to be used
     * @param pcharacter charactername to be displayed
     */
    public void changeplayer(String fromname, String toname, String pcharacter)
    {   
        Scanner wordScanner; //scanner to find a single word
        String name = ""; //name to hold the current names for each player
            //to check against
         
        //The information we'll want to display in the playerlist
        String newPlayersNames = toname + " ( " + pcharacter + " ) ";
        
        //go through the playerlist
        for(int i = 0; i < playernames.length; i++)
        {
            //look for the first word in the playerlist
            wordScanner = new Scanner(playernames[i]);
           if( wordScanner.hasNext() )
           {
                name = wordScanner.next();
           }
            
            //if this player name is that of the person changing their name
            if(name.equals(fromname))
            {
                //set their entry to their new info
                playernames[i] = newPlayersNames;
                //leave the loop since the name was found
                break;
            }
        }
        
        //update the playerlist
        playerListingjList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = playernames;
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        playerListing.setViewportView(playerListingjList);
    }

    
/**
     * Removes a player from the player listing.
     * @param splayer player to be removed.
     */
    public void removeplayer(String splayer)
    {
        
        String tempplayernames[] = new String[playernames.length-1];
        boolean found = false;
        for(int i = 0; i < playernames.length; i++)
        {
            //if(!playernames[i].equals(splayer))
            if(!playernames[i].contains(splayer))
            {
                //tempchatwindowoutput("got here");
                if(found == false)
                {
                    tempplayernames[i] = playernames[i];
                }
                if(found == true)
                {
                    tempplayernames[i-1] = playernames[i];
                }
            }
            else
            {
                found = true;
            }
        }
        
        playernames = tempplayernames;
        playerListingjList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = playernames;
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        playerListing.setViewportView(playerListingjList);
    }
    
///end Player Listing///
    
    
///Character sheet/// 
/**
     * Retrieves the text from the charactersheet window.
     * @return the charactersheet window text as one single string.
     */
    public String getcharsheet()
    {
        return characterSheetEditorPane.getText();
    }
    
    
/**
     * 
     */
    private void charactersheetbuttonpopup()
    {
        csMenu.add(makeMenuItemChar("Cut"));
        csMenu.add(makeMenuItemChar("Copy"));
        csMenu.add(makeMenuItemChar("Paste"));
        csMenu.add(makeMenuItemChar("Save As"));
        csMenu.add(makeMenuItemChar("Select All"));
        csMenu.add(makeMenuItemChar("Open"));
        characterSheetEditorPane.setComponentPopupMenu(csMenu);
    }
    
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private JMenuItem makeMenuItemChar(String label)
    {
        JMenuItem item = new JMenuItem(label);
        item.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                //enter code to be executed when clicked here
                
                charAH(evt);
            }
        });      
        return item;
    }
    
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void charAH(ActionEvent evt) 
    {        
        
        
        //Point temppoint = jList1.getMousePosition();
        //Object temp = jList1.getComponentAt(temppoint);
//        Object temp = jList1.getSelectedValue();

        
        String action = evt.getActionCommand(); 
        
        if (action.equals("Cut"))
        {
           // System.out.println("Cut");
            characterSheetEditorPane.cut();
        }
        else if (action.equals("Copy"))
        {
           // System.out.println("copy");
            characterSheetEditorPane.copy();
        }
        else if (action.equals("Paste"))
        {
           // System.out.println("paste");
            characterSheetEditorPane.paste();
        }
        else if (action.equals("Select All"))
        {
            //System.out.println("select all");
            characterSheetEditorPane.selectAll();
            characterSheetEditorPane.requestFocus();
        }
        else if (action.equals("Save As"))
        {
            //System.out.println("save as");
            savenotes(characterSheetEditorPane.getText());
        }
        else if (action.equals("Open"))
        {
           // System.out.println("open");
            String tempstring = opennotes();
            characterSheetEditorPane.setText(tempstring);
           // System.out.println(tempstring);
        }
        
    }
///end Character Sheet///
    
    
///Notepad///  
/**
     * 
     */
    private void createnotepadbuttonpopup()
    {
        npMenu.add(makeMenuItemNotes("Cut"));
        npMenu.add(makeMenuItemNotes("Copy"));
        npMenu.add(makeMenuItemNotes("Paste"));
        npMenu.add(makeMenuItemNotes("Save As"));
        npMenu.add(makeMenuItemNotes("Select All"));
        npMenu.add(makeMenuItemNotes("Open"));
        notesTextArea.setComponentPopupMenu(npMenu);
    }
    
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private JMenuItem makeMenuItemNotes(String label)
    {
        JMenuItem item = new JMenuItem(label);
        item.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                //enter code to be executed when clicked here
                        
                notesAH(evt);
            }
        });      
        return item;
    }
    
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void notesAH(ActionEvent evt) 
    {        
        
        
        String action = evt.getActionCommand(); 
        
        //System.out.println(action+" action");
        
        if (action.equals("Cut"))
        {
            //System.out.println("Cut");
            notesTextArea.cut();
        }
        else if (action.equals("Copy"))
        {
            //System.out.println("copy");
            notesTextArea.copy();
        }
        else if (action.equals("Paste"))
        {
            //System.out.println("paste");
            notesTextArea.paste();
        }
        else if (action.equals("Select All"))
        {
            //System.out.println("select all");
            notesTextArea.selectAll();
            notesTextArea.requestFocus();
        }
        else if (action.equals("Save As"))
        {
            //System.out.println("save as");
            savenotes(notesTextArea.getText());
        }
        else if (action.equals("Open"))
        {
            //System.out.println("open");
            String tempstring = opennotes();
            notesTextArea.setText(tempstring);
           // System.out.println(tempstring);
        }
        
    }
    
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private String opennotes()
    {
        int returnVal = notesfc.showOpenDialog(this);
        String temptext = "";
        if (returnVal == JFileChooser.APPROVE_OPTION) 
        {
            File tempfile = notesfc.getSelectedFile();
            
            String templocation = tempfile.getPath();

            //FileInputStream in; // declare a file output object
//            PrintStream p; // declare a print stream object

            try
            {
                // Open the file that is the first 
                // command line parameter
                
                //FileInputStream fstream = new FileInputStream(templocation);

                // Convert our input stream to a
                // DataInputStream
                //DataInputStream in = new DataInputStream(fstream);
                BufferedReader bin = new BufferedReader(new FileReader(tempfile));
                // Continue to read lines while 
                // there are still some left to read
                String templine = bin.readLine();
                while (templine != null)
                {
                        // Print file line to screen
                        //temptext = in.readUTF();
                        
                        if(templine != null)
                        {
                            temptext = temptext + templine + "\n";
                        }
                        templine = bin.readLine();
                        //System.out.println (in.readLine());
                }

                bin.close();

            }
            catch (Exception e)
            {
                    System.err.println ("Error reading file");
            }// TODO add your handling code here:

            //enter code to be executed when clicked here
            //notesTextArea.selectAll();
        }
        //System.out.println(temptext);
        return temptext;
    }
    
    
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void savenotes(String tosave)
    {
        int returnVal = notesfc.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) 
        {
            File tempfile = notesfc.getSelectedFile();
            String templocation = tempfile.getPath();
            if(notesfc.getFileFilter().equals(notesfc.getAcceptAllFileFilter()))
            {
            }
            else
                {
                if(templocation.endsWith(".txt"))
                {
                }
                else
                {
                    templocation = templocation + ".txt";
                }
            }


            FileOutputStream out; // declare a file output object
            PrintStream p; // declare a print stream object

            try
            {
                // Create a new file output stream
                // connected to "myfile.txt"
                out = new FileOutputStream(templocation);

                // Connect print stream to the output stream
                p = new PrintStream( out );
                
                p.print(tosave);
                //p.println ("This is written to a file");

                p.close();
            }
            catch (Exception e)
            {
                    System.err.println ("Error writing to file");
            }// TODO add your handling code here:

            //enter code to be executed when clicked here
            //notesTextArea.selectAll();
        }
    }
///end notepad    
    
    
    
///chat output   
/**
     * 
     */
    private void createchatoutputpopup()
    {
        JPopupMenu popup = new JPopupMenu(); //temporary storage to hold the menu while building it
        JMenuItem menuItem; //temporary storage to hold menu items while building them


        //begin building menu item
        menuItem = new JMenuItem("Copy");
        // menuItem.addActionListener(this);
        menuItem.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                //enter code to be executed when clicked here
                chatDisplayEditorPane.copy();
            }
        });      
        popup.add(menuItem);
        //end building menu item        
        
        
              
        //begin building menu item
        menuItem = new JMenuItem("Select All");
        // menuItem.addActionListener(this);
        menuItem.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                //enter code to be executed when clicked here
                chatDisplayEditorPane.selectAll();
            }
        });      
        popup.add(menuItem);
        //end building menu item        
        
        //attach menu to GUI component
        chatDisplayEditorPane.setComponentPopupMenu(popup);
        //jList1.setComponentPopupMenu(popup);
    } 
    
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private String makeheader()
    {
        String htmlheader = "<html>\n\t<head>\n\t\t<title>D20 Chat</title>\n" +
            "\t\t<style type=\"text/css\">\n\t\tbody {\n" +
            "\t\t\tfont-family:" + tempMain.theSettings.displayFont +
            ";\n\t\t\tfont-size:" + tempMain.theSettings.displaySize + 
                ";\n\t\t\tcolor:rgb(" + tempMain.theSettings.defaultColor.red + "," + 
                tempMain.theSettings.defaultColor.green + "," + 
                tempMain.theSettings.defaultColor.blue + ")" +
                       ";\n\t\t\tbackground:rgb(" + tempMain.theSettings.backColor.red + "," + 
                tempMain.theSettings.backColor.green + "," + 
                tempMain.theSettings.backColor.blue + ")" +
            ";\n\t\t}\n\t\t</style>\n\t</head>\n\t<body>\n";
        return htmlheader;
    }
    
    
/**
     * Adds a string to the chat buffer and refreshes the chat display from the buffer.
     * Consider renaming to something more meaningful.
     * @param message string to be added to the chat buffer.
     */
    public void tempchatwindowoutput(String message)
    {
        String temp = chatDisplayEditorPane.getText();
        String htmlheader = makeheader();
        buffer.addline(message + "<br />\n");
        //content = content + message + "<br>\n";
        chatDisplayEditorPane.setText(htmlheader + buffer.gettext() + "<a name='end'>\n<br />\n\t</body>\n</html>\n");
        Document doc = chatDisplayEditorPane.getDocument();
        chatDisplayEditorPane.setCaretPosition(doc.getLength());

    }    
///end chat output///    
    
    
///chat input///    
/**
     * 
     */
    private void createchatinputpopup()
    {
        JPopupMenu popup = new JPopupMenu(); //temporary storage to hold the menu while building it
        JMenuItem menuItem; //temporary storage to hold menu items while building them

        //begin building menu item
        menuItem = new JMenuItem("Cut");
        // menuItem.addActionListener(this);
        menuItem.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                //enter code to be executed when clicked here
                chatEntryTextArea.cut();
            }
        });      
        popup.add(menuItem);
        //end building menu item

        //begin building menu item
        menuItem = new JMenuItem("Copy");
        // menuItem.addActionListener(this);
        menuItem.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                //enter code to be executed when clicked here
                chatEntryTextArea.copy();
            }
        });      
        popup.add(menuItem);
        //end building menu item        
        
        
        //begin building menu item
        menuItem = new JMenuItem("Paste");
        // menuItem.addActionListener(this);
        menuItem.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                //enter code to be executed when clicked here
                chatEntryTextArea.paste();
            }
        });      
        popup.add(menuItem);
        //end building menu item              
        
                
        //begin building menu item
        menuItem = new JMenuItem("Select All");
        // menuItem.addActionListener(this);
        menuItem.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                //enter code to be executed when clicked here
                chatEntryTextArea.selectAll();
            }
        });      
        popup.add(menuItem);
        //end building menu item        
        
        //attach menu to GUI component
        chatEntryTextArea.setComponentPopupMenu(popup);
        //jList1.setComponentPopupMenu(popup);
    }
///end chat input///    
    

/**
     * Closes client connection, and shuts down the program.
     * After asking the client to shut down it waits a little bit, and then it
     * shuts everything down. This allows the client to have a few moments to inform
     * the server that it is leaving (instead of just simply disapearing)
     * @param e Window event attached to the program shutting down (not really used here).
     */
    public void windowClosing( WindowEvent e )   
    {
        //so far this doesn't seem to actually work
        tempchatwindowoutput ( "Closing connection and quitting\n" );
        tempclient.CloseConnection ();
        try {Thread.sleep ( 2000 );} catch ( InterruptedException except) {;}
        dispose ();
        System.exit ( 0 );
    }

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
/**
     * 
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        mainHorizontalSplitPane = new javax.swing.JSplitPane();
        chatPanel = new javax.swing.JPanel();
        chatDisplayScrollPane = new javax.swing.JScrollPane();
        chatDisplayEditorPane = new javax.swing.JEditorPane();
        chatEntryScrollPane = new javax.swing.JScrollPane();
        chatEntryTextArea = new javax.swing.JTextArea();
        chatSendButton = new javax.swing.JButton();
        d2Button = new javax.swing.JButton();
        d4Button = new javax.swing.JButton();
        d6Button = new javax.swing.JButton();
        d8Button = new javax.swing.JButton();
        d10Button = new javax.swing.JButton();
        d12Button = new javax.swing.JButton();
        d20Button = new javax.swing.JButton();
        d100Button = new javax.swing.JButton();
        chatSaveButton = new javax.swing.JButton();
        topVerticleSplitPane = new javax.swing.JSplitPane();
        bottomVerticleSplitPane = new javax.swing.JSplitPane();
        notesPanel = new javax.swing.JPanel();
        notesLabel = new javax.swing.JLabel();
        notesMenuButton = new javax.swing.JButton();
        notesScrollPane = new javax.swing.JScrollPane();
        notesTextArea = new javax.swing.JTextArea();
        characterSheetsPanel = new javax.swing.JPanel();
        characterNameLabel = new javax.swing.JLabel();
        characterSheetButton = new javax.swing.JButton();
        characterSheetScrollPane = new javax.swing.JScrollPane();
        characterSheetEditorPane = new javax.swing.JEditorPane();
        characterNameTextField = new javax.swing.JTextField();
        playersPanel = new javax.swing.JPanel();
        playersLabel = new javax.swing.JLabel();
        playersMenuButton = new javax.swing.JButton();
        playerListing = new javax.swing.JScrollPane();
        playerListingjList = new javax.swing.JList();
        mainMenuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        startServer = new javax.swing.JMenuItem();
        stopServer = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        connectServer = new javax.swing.JMenuItem();
        disconnectServer = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        saveTranscript = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JSeparator();
        exit = new javax.swing.JMenuItem();
        optionsMenu = new javax.swing.JMenu();
        profileManager = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JSeparator();
        playerOptions = new javax.swing.JMenuItem();
        gmOptions = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        help = new javax.swing.JMenuItem();
        about = new javax.swing.JMenuItem();
        Commands = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("D20 Chat");
        mainHorizontalSplitPane.setDividerLocation(250);
        chatDisplayEditorPane.setEditable(false);
        chatDisplayEditorPane.setContentType("text/html");
        chatDisplayScrollPane.setViewportView(chatDisplayEditorPane);

        chatEntryTextArea.setColumns(20);
        chatEntryTextArea.setLineWrap(true);
        chatEntryTextArea.setRows(5);
        chatEntryTextArea.setWrapStyleWord(true);
        chatEntryTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                chatEntryTextAreaKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                chatEntryTextAreaKeyReleased(evt);
            }
        });

        chatEntryScrollPane.setViewportView(chatEntryTextArea);

        chatSendButton.setText("Send");
        chatSendButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                chatSendButtonMouseClicked(evt);
            }
        });
        chatSendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chatSendButtonActionPerformed(evt);
            }
        });

        d2Button.setText("1d2");
        d2Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                d2ButtonActionPerformed(evt);
            }
        });

        d4Button.setText("1d4");
        d4Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                d4ButtonActionPerformed(evt);
            }
        });

        d6Button.setText("1d6");
        d6Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                d6ButtonActionPerformed(evt);
            }
        });

        d8Button.setText("1d8");
        d8Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                d8ButtonActionPerformed(evt);
            }
        });

        d10Button.setText("1d10");
        d10Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                d10ButtonActionPerformed(evt);
            }
        });

        d12Button.setText("1d12");
        d12Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                d12ButtonActionPerformed(evt);
            }
        });

        d20Button.setText("1d20");
        d20Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                d20ButtonActionPerformed(evt);
            }
        });

        d100Button.setText("1d100");
        d100Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                d100ButtonActionPerformed(evt);
            }
        });

        chatSaveButton.setText("Save");
        chatSaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chatSaveButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout chatPanelLayout = new org.jdesktop.layout.GroupLayout(chatPanel);
        chatPanel.setLayout(chatPanelLayout);
        chatPanelLayout.setHorizontalGroup(
            chatPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, chatPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(chatPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, chatDisplayScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, chatPanelLayout.createSequentialGroup()
                        .add(chatEntryScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 487, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(chatPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(chatSaveButton)
                            .add(chatSendButton)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, chatPanelLayout.createSequentialGroup()
                        .add(d2Button)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(d4Button)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(d6Button)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(d8Button)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(d10Button)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(d12Button)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(d20Button)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(d100Button)))
                .addContainerGap())
        );
        chatPanelLayout.setVerticalGroup(
            chatPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, chatPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(chatDisplayScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(chatPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(d2Button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(d4Button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(d6Button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(d8Button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(d10Button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(d12Button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(d20Button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(d100Button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(chatPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, chatPanelLayout.createSequentialGroup()
                        .add(chatSaveButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(chatSendButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(chatEntryScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 96, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        mainHorizontalSplitPane.setRightComponent(chatPanel);

        topVerticleSplitPane.setDividerLocation(150);
        topVerticleSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        bottomVerticleSplitPane.setDividerLocation(200);
        bottomVerticleSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        notesLabel.setText("Notes");

        notesMenuButton.setText("Notes Menu");
        notesMenuButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                notesMenuButtonMouseClicked(evt);
            }
        });
        notesMenuButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                notesMenuButtonActionPerformed(evt);
            }
        });

        notesTextArea.setLineWrap(true);
        notesScrollPane.setViewportView(notesTextArea);

        org.jdesktop.layout.GroupLayout notesPanelLayout = new org.jdesktop.layout.GroupLayout(notesPanel);
        notesPanel.setLayout(notesPanelLayout);
        notesPanelLayout.setHorizontalGroup(
            notesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, notesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(notesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, notesScrollPane)
                    .add(notesPanelLayout.createSequentialGroup()
                        .add(notesLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 108, Short.MAX_VALUE)
                        .add(notesMenuButton)))
                .addContainerGap())
        );
        notesPanelLayout.setVerticalGroup(
            notesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(notesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(notesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(notesLabel)
                    .add(notesMenuButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(notesScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                .addContainerGap())
        );
        bottomVerticleSplitPane.setBottomComponent(notesPanel);

        characterNameLabel.setText("Character name:");

        characterSheetButton.setText("Char Menu");
        characterSheetButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                characterSheetButtonMouseClicked(evt);
            }
        });
        characterSheetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                characterSheetButtonActionPerformed(evt);
            }
        });

        characterSheetScrollPane.setViewportView(characterSheetEditorPane);

        characterNameTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                characterNameTextFieldFocusLost(evt);
            }
        });
        characterNameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                characterNameTextFieldKeyReleased(evt);
            }
        });

        org.jdesktop.layout.GroupLayout characterSheetsPanelLayout = new org.jdesktop.layout.GroupLayout(characterSheetsPanel);
        characterSheetsPanel.setLayout(characterSheetsPanelLayout);
        characterSheetsPanelLayout.setHorizontalGroup(
            characterSheetsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(characterSheetsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(characterSheetsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(characterSheetScrollPane)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, characterSheetsPanelLayout.createSequentialGroup()
                        .add(characterNameLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(characterNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, characterSheetButton))
                .addContainerGap())
        );
        characterSheetsPanelLayout.setVerticalGroup(
            characterSheetsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(characterSheetsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(characterSheetButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(characterSheetsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(characterNameLabel)
                    .add(characterNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(9, 9, 9)
                .add(characterSheetScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                .addContainerGap())
        );
        bottomVerticleSplitPane.setTopComponent(characterSheetsPanel);

        topVerticleSplitPane.setRightComponent(bottomVerticleSplitPane);

        playersPanel.setPreferredSize(new java.awt.Dimension(0, 200));
        playersLabel.setText("Players");

        playersMenuButton.setText("Players Menu");
        playersMenuButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                playersMenuButtonMouseClicked(evt);
            }
        });

        playerListingjList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                playerListingjListMouseClicked(evt);
            }
        });

        playerListing.setViewportView(playerListingjList);

        org.jdesktop.layout.GroupLayout playersPanelLayout = new org.jdesktop.layout.GroupLayout(playersPanel);
        playersPanel.setLayout(playersPanelLayout);
        playersPanelLayout.setHorizontalGroup(
            playersPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, playersPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(playersPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, playerListing, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                    .add(playersPanelLayout.createSequentialGroup()
                        .add(playersLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 95, Short.MAX_VALUE)
                        .add(playersMenuButton)))
                .addContainerGap())
        );
        playersPanelLayout.setVerticalGroup(
            playersPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(playersPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(playersPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(playersMenuButton)
                    .add(playersLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(playerListing, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                .addContainerGap())
        );
        topVerticleSplitPane.setLeftComponent(playersPanel);

        mainHorizontalSplitPane.setLeftComponent(topVerticleSplitPane);

        fileMenu.setText("File");
        fileMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuActionPerformed(evt);
            }
        });

        startServer.setText("Start Server");
        startServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startServerActionPerformed(evt);
            }
        });

        fileMenu.add(startServer);

        stopServer.setText("Stop Server");
        stopServer.setEnabled(false);
        stopServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopServerActionPerformed(evt);
            }
        });

        fileMenu.add(stopServer);

        fileMenu.add(jSeparator1);

        connectServer.setText("Connect to Server");
        connectServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectServerActionPerformed(evt);
            }
        });

        fileMenu.add(connectServer);

        disconnectServer.setText("Disconnect from Server");
        disconnectServer.setEnabled(false);
        disconnectServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                disconnectServerActionPerformed(evt);
            }
        });

        fileMenu.add(disconnectServer);

        fileMenu.add(jSeparator3);

        saveTranscript.setText("Save Transcript");
        saveTranscript.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveTranscriptActionPerformed(evt);
            }
        });

        fileMenu.add(saveTranscript);

        fileMenu.add(jSeparator4);

        exit.setText("Exit");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });

        fileMenu.add(exit);

        mainMenuBar.add(fileMenu);

        optionsMenu.setText("Options");
        profileManager.setText("Profile Manager");
        profileManager.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                profileManagerActionPerformed(evt);
            }
        });

        optionsMenu.add(profileManager);

        optionsMenu.add(jSeparator5);

        playerOptions.setText("User Options");
        playerOptions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                playerOptionsActionPerformed(evt);
            }
        });

        optionsMenu.add(playerOptions);

        gmOptions.setText("GM Options");
        gmOptions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gmOptionsActionPerformed(evt);
            }
        });

        optionsMenu.add(gmOptions);

        mainMenuBar.add(optionsMenu);

        helpMenu.setText("Help");
        helpMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpMenuActionPerformed(evt);
            }
        });

        help.setText("SourceForge Help");
        help.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpActionPerformed(evt);
            }
        });

        helpMenu.add(help);

        about.setText("About D20 Chat");
        about.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutActionPerformed(evt);
            }
        });

        helpMenu.add(about);

        Commands.setText("D20 Chat Text Commands");
        Commands.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CommandsActionPerformed(evt);
            }
        });

        helpMenu.add(Commands);

        mainMenuBar.add(helpMenu);

        setJMenuBar(mainMenuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainHorizontalSplitPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 826, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainHorizontalSplitPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 597, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void characterNameTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_characterNameTextFieldFocusLost
        String name = characterNameTextField.getText();
        if( name.length() != 0)
        {
            tempMain.theUser.setCharacterName( name );
            OutParse.sendMessage("/charNameChange " + tempMain.theUser.getCharacterName());

        }
    }//GEN-LAST:event_characterNameTextFieldFocusLost

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void gmOptionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gmOptionsActionPerformed
        // Pop up EditProfile form
        GmOptions gmOptions;
        gmOptions = new GmOptions(tempMain);
        //gmOptions.tempMain = tempMain;
        
        gmOptions.setVisible(true);
    }//GEN-LAST:event_gmOptionsActionPerformed

    
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void characterNameTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_characterNameTextFieldKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) 
        {
            String name = characterNameTextField.getText();
            if( name.length() != 0)
            {
                tempMain.theUser.setCharacterName( name );
                OutParse.sendMessage("/charNameChange " + tempMain.theUser.getCharacterName());
                
            }
        }
            
    }//GEN-LAST:event_characterNameTextFieldKeyReleased

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void CommandsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CommandsActionPerformed
        // Pop up help form
        Commands commandsForm;
        commandsForm = new Commands();
        //commandsForm.tempMain = tempMain;
        
        commandsForm.setVisible(true);
    }//GEN-LAST:event_CommandsActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void helpMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpMenuActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_helpMenuActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void notesMenuButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_notesMenuButtonMouseClicked
        selectedComponent = evt.getComponent();
        npMenu.show(evt.getComponent(), evt.getX(), evt.getY());
    }//GEN-LAST:event_notesMenuButtonMouseClicked

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void characterSheetButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_characterSheetButtonMouseClicked
        selectedComponent = evt.getComponent();
        csMenu.show(evt.getComponent(), evt.getX(), evt.getY());
    }//GEN-LAST:event_characterSheetButtonMouseClicked

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void playersMenuButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_playersMenuButtonMouseClicked
        selectedComponent = evt.getComponent();
        pbMenu.show(evt.getComponent(), evt.getX(), evt.getY());
    }//GEN-LAST:event_playersMenuButtonMouseClicked
   
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/  
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void profileManagerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_profileManagerActionPerformed
        // Pop up EditProfile form
        ProfileManager profileManager;
        profileManager = new ProfileManager();
        profileManager.tempMain = tempMain;
        
        profileManager.setVisible(true);
    }//GEN-LAST:event_profileManagerActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void d100ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_d100ButtonActionPerformed
printCleanDice("1d100");
    }//GEN-LAST:event_d100ButtonActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutActionPerformed
        // Pop up about form
        About aboutForm;
        aboutForm = new About();
        //aboutForm.tempMain = tempMain;
        
        aboutForm.setVisible(true);
    }//GEN-LAST:event_aboutActionPerformed

    
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void helpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpActionPerformed
        // Pop up help form
        Help helpForm;
        helpForm = new Help();
        //helpForm.tempMain = tempMain;
        
        helpForm.setVisible(true);
    }//GEN-LAST:event_helpActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void saveTranscriptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveTranscriptActionPerformed


        int returnVal = fc.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) 
        {
            File tempfile = fc.getSelectedFile();
            String templocation = tempfile.getPath();
            if(fc.getFileFilter().equals(fc.getAcceptAllFileFilter()))
            {
            }
            else
            {
                if(templocation.endsWith(".html") || templocation.endsWith(".htm"))
                {
                }
                else
                {
                    templocation = templocation + ".html";
                }
            }


            FileOutputStream out; // declare a file output object
            PrintStream p; // declare a print stream object
            String htmlheader = makeheader();
            try
            {
                // Create a new file output stream
                // connected to "myfile.txt"
                out = new FileOutputStream(templocation);

                // Connect print stream to the output stream
                p = new PrintStream( out );
                Document doc = chatDisplayEditorPane.getDocument();
                p.print(htmlheader + buffer.getall() + "<a name='end'>\n<br />\n\t</body>\n</html>\n");
                //p.println ("This is written to a file");

                p.close();
            }
            catch (Exception e)
            {
                    System.err.println ("Error writing to file");
            }// TODO add your handling code here:
        }// TODO add your handling code here:
    }//GEN-LAST:event_saveTranscriptActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void chatSaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chatSaveButtonActionPerformed


        int returnVal = fc.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) 
        {
            File tempfile = fc.getSelectedFile();
            String templocation = tempfile.getPath();
            if(fc.getFileFilter().equals(fc.getAcceptAllFileFilter()))
            {
            }
            else
            {
                if(templocation.endsWith(".html") || templocation.endsWith(".htm"))
                {
                }
                else
                {
                    templocation = templocation + ".html";
                }
            }

            FileOutputStream out; // declare a file output object
            PrintStream p; // declare a print stream object
            String htmlheader = makeheader();
            try
            {
                // Create a new file output stream
                // connected to "myfile.txt"
                out = new FileOutputStream(templocation);

                // Connect print stream to the output stream
                p = new PrintStream( out );
                Document doc = chatDisplayEditorPane.getDocument();
                p.print(htmlheader + buffer.getall() + "<a name='end'>\n<br />\n\t</body>\n</html>\n");
                //p.println ("This is written to a file");

                p.close();
            }
            catch (Exception e)
            {
                System.err.println ("Error writing to file");
            }// TODO add your handling code here:
        }
    }//GEN-LAST:event_chatSaveButtonActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void playerListingjListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_playerListingjListMouseClicked

    }//GEN-LAST:event_playerListingjListMouseClicked

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void printCleanDice(String dice)
    {
        
        String tempstring = chatEntryTextArea.getText();
        int position = chatEntryTextArea.getCaretPosition();
        char tempchar;
        for(int i = position - 1; i >= 0; i--)
        {
            tempchar = tempstring.charAt(i);
            if(tempchar == '[')
            {
                chatEntryTextArea.insert(dice, chatEntryTextArea.getCaretPosition());
                chatEntryTextArea.requestFocus();
                return;
            }
            else if(tempchar == ']')
            {
                chatEntryTextArea.insert("["+dice+"]", chatEntryTextArea.getCaretPosition());
                chatEntryTextArea.requestFocus();
                return;
            }
        }
        
        chatEntryTextArea.insert("["+dice+"]", chatEntryTextArea.getCaretPosition());
        chatEntryTextArea.requestFocus();        
    }
  
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void d2ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_d2ButtonActionPerformed
        printCleanDice("1d2");
// TODO add your handling code here:
    }//GEN-LAST:event_d2ButtonActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void disconnectServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_disconnectServerActionPerformed
        // Disconnect from server, check if connected, then re-enable
        //  "Connect to Server" from "File" menu and re-disable
        //  "Disconnect from Server"
        tempclient.CloseConnection();
        
        //if (!tempMain.connected) {
            // Not connnected - re-enable "Connect to Server" from
            //  "File" menu and re-disable "Disconnect from Server"
            //this.showMenuConnectToServer(true);
            //this.showMenuDisconnectToServer(false);
        //}
    }//GEN-LAST:event_disconnectServerActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void stopServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopServerActionPerformed
        // Stop server, check if server is stopped, then re-enable
        //  "Start Server" from "File" menu and re-disable "Stop Server"
        //tempserver.Shutdown();
        if( tempMain.connected == true )
        {
            tempclient.CloseConnection();
        }
        tempMain.theserver.Shutdown();
        //if (!tempMain.isServer) {
            // Server not running - re-enable "Start Server" from
            //  "File" menu and re-disable "Stop Server"
            //this.showMenuStartServer(true);
            //this.showMenuStopServer(false);            
        //}
    }//GEN-LAST:event_stopServerActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void chatEntryTextAreaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_chatEntryTextAreaKeyPressed

    }//GEN-LAST:event_chatEntryTextAreaKeyPressed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void chatSendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chatSendButtonActionPerformed

        
        // Parse and send message 
        if(chatEntryTextArea.getText().length() != 0)
        {
        OutParse.sendMessage(chatEntryTextArea.getText());
        }
        // Clear ChatEntryTextArea then give it the focus
        chatEntryTextArea.setText(null);
        chatEntryTextArea.requestFocus();
    }//GEN-LAST:event_chatSendButtonActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/  
    private void chatEntryTextAreaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_chatEntryTextAreaKeyReleased
        // Check for key characters
        if (evt.getKeyChar() == 'a') {
            //process(evt.getKeyChar());
        }
        
        if (evt.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK)
        {
            if (evt.getKeyCode() == KeyEvent.VK_T)
            {
                chatEntryTextArea.setText("/tell  " + chatEntryTextArea.getText());
                chatEntryTextArea.setCaretPosition(6);
            }
        }

        if (evt.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK)
        {
            if (evt.getKeyCode() == KeyEvent.VK_R)
            {
                chatEntryTextArea.setText("/tell " + tempMain.InParse.lastPerson + " " + chatEntryTextArea.getText());
            }
        }
        /*if (evt.getModifiersEx() == 64)
        {
            notesTextArea.setText(Integer.toString(KeyEvent.VK_SHIFT));
            return;
        }*/
        // Check for key codes
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) 
        {
            // Do Send button's action
            String tempmess = chatEntryTextArea.getText();
           
            int position = chatEntryTextArea.getCaretPosition();
            
            
            if (evt.getModifiersEx() == KeyEvent.SHIFT_DOWN_MASK)
            {
                if( position != tempmess.length() )
                {
                    tempmess = tempmess.substring(0, position) + tempmess.substring( position, tempmess.length());

                }
                else
                {
                    tempmess = tempmess.substring(0, tempmess.length());
                }
            }
            else
            {
                if( position != tempmess.length() )
                {
                    tempmess = tempmess.substring(0, position-1) + tempmess.substring( position, tempmess.length());

                }
                else
                {
                    tempmess = tempmess.substring(0, tempmess.length()-1);
                }                
            }
            
            if(tempmess.length() != 0)
            {
            OutParse.sendMessage(tempmess);
            }
            // Clear ChatEntryTextArea then give it the focus
            chatEntryTextArea.setText(null);
            chatEntryTextArea.requestFocus();
        }
        else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) 
        {
            chatEntryTextArea.setText(null);
            chatEntryTextArea.requestFocus();
        }
    }//GEN-LAST:event_chatEntryTextAreaKeyReleased

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void playerOptionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playerOptionsActionPerformed
        // Pop up Options form
        UserOptions userOptions;
        userOptions = new UserOptions();
        userOptions.tempMain = tempMain;
        
        userOptions.setVisible(true);
    }//GEN-LAST:event_playerOptionsActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void startServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startServerActionPerformed
        // Pop up StartServer form
        StartServer startServer;
        startServer = new StartServer( tempMain );
        //startServer.tempMain = tempMain;
        //startServer.tempGui = this;
        
        startServer.setVisible(true);
    }//GEN-LAST:event_startServerActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileMenuActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_fileMenuActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed

        System.exit(0);
    }//GEN-LAST:event_exitActionPerformed

    
    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void connectServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectServerActionPerformed
        // Pop up ConnectToServer form
        ConnectToServer serverConnect;
        serverConnect = new ConnectToServer(this, tempMain);
        //serverConnect.tempMain = tempMain;
        //serverConnect.theGUI = this;
        
        serverConnect.setVisible(true);
    }//GEN-LAST:event_connectServerActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/    
    private void chatSendButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_chatSendButtonMouseClicked
// TODO add your handling code here:
        
    }//GEN-LAST:event_chatSendButtonMouseClicked

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void d20ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_d20ButtonActionPerformed
printCleanDice("1d20");
    }//GEN-LAST:event_d20ButtonActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void d12ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_d12ButtonActionPerformed
printCleanDice("1d12");
// TODO add your handling code here:
    }//GEN-LAST:event_d12ButtonActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void d10ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_d10ButtonActionPerformed
printCleanDice("1d10");
// TODO add your handling code here:
    }//GEN-LAST:event_d10ButtonActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void d8ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_d8ButtonActionPerformed
printCleanDice("1d8");
// TODO add your handling code here:
    }//GEN-LAST:event_d8ButtonActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void d6ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_d6ButtonActionPerformed
printCleanDice("1d6");
// TODO add your handling code here:
    }//GEN-LAST:event_d6ButtonActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void d4ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_d4ButtonActionPerformed
printCleanDice("1d4");
// TODO add your handling code here:
    }//GEN-LAST:event_d4ButtonActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void characterSheetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_characterSheetButtonActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_characterSheetButtonActionPerformed

    
/********************************************************************************
  Class:
  Description:
  Used by:
  Uses:
********************************************************************************/
    private void notesMenuButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_notesMenuButtonActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_notesMenuButtonActionPerformed
 
    
    
/**
     * Toggles the menu item Start Server.
     * @param option toggle if menu item start server should be greyed or not.
     */
    public void showMenuStartServer(boolean option) {
        this.startServer.setEnabled(option);
    }
  
     
/**
     * Toggles the menu item Stop Server.
     * @param option toggle if menu item stop server should be greyed or not.
     */
    public void showMenuStopServer(boolean option) {
        this.stopServer.setEnabled(option);
    }
   
     
/**
     * Toggles the menu item connect to server.
     * @param option toggle if menu item connect to server should be greyed or not.
     */
    public void showMenuConnectToServer(boolean option) {
        this.connectServer.setEnabled(option);
    }
   
     
/**
     * Toggles the menu item disconnect from Server.
     * @param option toggle if menu item disconnect from server should be greyed or not.
     */
    public void showMenuDisconnectToServer(boolean option) {
        this.disconnectServer.setEnabled(option);
    }
   
    
/**
     * Changes the title bar of the main GUI.
     * Typically used to display if the computer is hosting and the current address.
     * @param title New title to be displayed
     */
    public void changeGuiTitle(String title) {
        this.setTitle(title);
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Commands;
    private javax.swing.JMenuItem about;
    private javax.swing.JSplitPane bottomVerticleSplitPane;
    private javax.swing.JLabel characterNameLabel;
    private javax.swing.JTextField characterNameTextField;
    private javax.swing.JButton characterSheetButton;
    private javax.swing.JEditorPane characterSheetEditorPane;
    private javax.swing.JScrollPane characterSheetScrollPane;
    private javax.swing.JPanel characterSheetsPanel;
    private javax.swing.JEditorPane chatDisplayEditorPane;
    private javax.swing.JScrollPane chatDisplayScrollPane;
    private javax.swing.JScrollPane chatEntryScrollPane;
    private javax.swing.JTextArea chatEntryTextArea;
    private javax.swing.JPanel chatPanel;
    private javax.swing.JButton chatSaveButton;
    private javax.swing.JButton chatSendButton;
    private javax.swing.JMenuItem connectServer;
    private javax.swing.JButton d100Button;
    private javax.swing.JButton d10Button;
    private javax.swing.JButton d12Button;
    private javax.swing.JButton d20Button;
    private javax.swing.JButton d2Button;
    private javax.swing.JButton d4Button;
    private javax.swing.JButton d6Button;
    private javax.swing.JButton d8Button;
    private javax.swing.JMenuItem disconnectServer;
    private javax.swing.JMenuItem exit;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenuItem gmOptions;
    private javax.swing.JMenuItem help;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSplitPane mainHorizontalSplitPane;
    private javax.swing.JMenuBar mainMenuBar;
    private javax.swing.JLabel notesLabel;
    private javax.swing.JButton notesMenuButton;
    private javax.swing.JPanel notesPanel;
    private javax.swing.JScrollPane notesScrollPane;
    private javax.swing.JTextArea notesTextArea;
    private javax.swing.JMenu optionsMenu;
    private javax.swing.JScrollPane playerListing;
    private javax.swing.JList playerListingjList;
    private javax.swing.JMenuItem playerOptions;
    private javax.swing.JLabel playersLabel;
    private javax.swing.JButton playersMenuButton;
    private javax.swing.JPanel playersPanel;
    private javax.swing.JMenuItem profileManager;
    private javax.swing.JMenuItem saveTranscript;
    private javax.swing.JMenuItem startServer;
    private javax.swing.JMenuItem stopServer;
    private javax.swing.JSplitPane topVerticleSplitPane;
    // End of variables declaration//GEN-END:variables
}
